package com.zcj.test.guide;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;


public class MainActivity extends Activity {
    private Activity context = this;
    private ArrayList<Guide> mGuideList;


    private static final String TAG = "MainActivity";
    private LinearLayout tv1;
    private LinearLayout tv2;
    private LinearLayout tv3;
    private LinearLayout tv4;
    private ImageView iv1;
    private ImageView iv2;
    private ImageView iv3;
    private ImageView iv4;
    private Button bt;
    private void assignViews() {
        tv1 = (LinearLayout) findViewById(R.id.tv1);
        tv2 = (LinearLayout) findViewById(R.id.tv2);
        tv3 = (LinearLayout) findViewById(R.id.tv3);
        tv4 = (LinearLayout) findViewById(R.id.tv4);
        iv1 = (ImageView) findViewById(R.id.iv1);
        iv2 = (ImageView) findViewById(R.id.iv2);
        iv3 = (ImageView) findViewById(R.id.iv3);
        iv4 = (ImageView) findViewById(R.id.iv4);
        bt = (Button) findViewById(R.id.bt);
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        assignViews();
        prepareGuide();
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d(TAG, "onClick: ");
                showTv1();
            }
        });
    }

    public void showTv1() {
        GuideBuilder builder = new GuideBuilder();
        builder.setTargetView(iv1)
                .setAlpha(150)
                .setHighTargetGraphStyle(Component.CIRCLE)
                .setHighTargetCorner(20)
                .setOverlayTarget(false)
                .setOutsideTouchable(false);
        builder.setOnVisibilityChangedListener(new GuideBuilder.OnVisibilityChangedListener() {
            @Override
            public void onShown() {
            }

            @Override
            public void onDismiss() {
                showTv2();
            }
        });

        builder.addComponent(new MutiComponent(1, 0));
        Guide guide = builder.createGuide();
        guide.setShouldCheckLocInWindow(false);
        guide.show(context);
        mGuideList.add(guide);
    }

    public void showTv2() {
        GuideBuilder builder1 = new GuideBuilder();
        builder1.setTargetView(iv2)
                .setAlpha(150)
                .setHighTargetGraphStyle(Component.CIRCLE)
                .setHighTargetCorner(20)
                .setOverlayTarget(false)
                .setOutsideTouchable(false);
        builder1.setOnVisibilityChangedListener(new GuideBuilder.OnVisibilityChangedListener() {
            @Override
            public void onShown() {
            }

            @Override
            public void onDismiss() {
                showTv3();
            }
        });

        builder1.addComponent(new MutiComponent(2, 0));
        Guide guide = builder1.createGuide();
        guide.setShouldCheckLocInWindow(false);
        guide.show(context);
        mGuideList.add(guide);
    }

    public void showTv3() {
        Guide guide;
        GuideBuilder builder1 = new GuideBuilder();
        builder1.setTargetView(iv3)
                .setAlpha(150)
                .setHighTargetGraphStyle(Component.CIRCLE)
                .setOverlayTarget(false)
                .setOutsideTouchable(false);
        builder1.setOnVisibilityChangedListener(new GuideBuilder.OnVisibilityChangedListener() {
            @Override
            public void onShown() {
            }

            @Override
            public void onDismiss() {
                showTv4();
            }
        });
        builder1.addComponent(new MutiComponent(3, 0));
        guide = builder1.createGuide();
        guide.setShouldCheckLocInWindow(false);
        guide.show(context);
        mGuideList.add(guide);
    }

    public void showTv4() {
        final GuideBuilder builder1 = new GuideBuilder();
        builder1.setTargetView(iv4)
                .setAlpha(150)
                .setHighTargetGraphStyle(Component.CIRCLE)
                .setOverlayTarget(false)
                .setOutsideTouchable(false);
        builder1.setOnVisibilityChangedListener(new GuideBuilder.OnVisibilityChangedListener() {
            @Override
            public void onShown() {
            }

            @Override
            public void onDismiss() {
                dissmissGuide();
            }
        });
        builder1.addComponent(new MutiComponent(4, 0));
        Guide guide = builder1.createGuide();
        guide.setShouldCheckLocInWindow(false);
        guide.show(context);
        mGuideList.add(guide);
    }

    private void prepareGuide() {
        mGuideList = new ArrayList<>();
        getWindow()
                .getDecorView()
                .getViewTreeObserver()
                .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                            getWindow()
                                    .getDecorView()
                                    .getViewTreeObserver()
                                    .removeOnGlobalLayoutListener(this);
                        } else {
                            getWindow()
                                    .getDecorView()
                                    .getViewTreeObserver()
                                    .removeGlobalOnLayoutListener(this);
                        }
                        showTv1();
                    }
                });
    }

    private void dissmissGuide() {
        if (mGuideList != null && mGuideList.size() != 0) {
            for (Guide guide : mGuideList) {
                guide.dismiss();
            }
            mGuideList.clear();
        }
    }

}